<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.order_number"
        placeholder="订单号"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.status" placeholder="订单状态" clearable style="width: 120px" class="filter-item">
        <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-select v-model="listQuery.logistics_company_id" placeholder="物流公司" clearable style="width: 150px" class="filter-item">
        <el-option v-for="item in logisticsCompanies" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        创建订单
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" width="180">
        <template slot-scope="scope">
          <span class="link-type" @click="handleDetail(scope.row)">{{ scope.row.order_no }}</span>
        </template>
      </el-table-column>
      <el-table-column label="寄件人" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.sender_name }}
        </template>
      </el-table-column>
      <el-table-column label="收件人" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.receiver_name }}
        </template>
      </el-table-column>
      <el-table-column label="物流公司" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.logistics_company ? scope.row.logistics_company.name : '未知' }}
        </template>
      </el-table-column>
      <el-table-column label="订单状态" width="120" align="center">
        <template slot-scope="scope">
          <el-tag :type="statusTypeMap[scope.row.status]">{{ statusLabelMap[scope.row.status] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" width="120" align="center">
        <template slot-scope="scope">
          ¥{{ scope.row.total_amount }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180" align="center">
        <template slot-scope="scope">
          {{ scope.row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetail(row)">
            详情
          </el-button>
          <el-button type="success" size="mini" @click="handleUpdateStatus(row)" :disabled="row.status === 'delivered'">
            更新状态
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <!-- 状态更新对话框 -->
    <el-dialog title="更新订单状态" :visible.sync="statusDialogVisible">
      <el-form
        ref="statusForm"
        :model="statusForm"
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="当前状态">
          <el-tag :type="statusTypeMap[currentStatus]">{{ statusLabelMap[currentStatus] }}</el-tag>
        </el-form-item>
        <el-form-item label="新状态" prop="status">
          <el-select v-model="statusForm.status" placeholder="请选择新状态">
            <el-option
              v-for="(label, value) in nextStatusOptions"
              :key="value"
              :label="label"
              :value="value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="statusForm.remark" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="statusDialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="submitStatusUpdate">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { http } from '@/api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { parseTime } from '@/utils'

export default {
  name: 'ExpressOrderList',
  components: { Pagination },
  directives: { waves },
  filters: {
    parseTime
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        order_number: undefined,
        status: undefined,
        logistics_company_id: undefined
      },
      logisticsCompanies: [],
      statusOptions: [
        { value: 'pending', label: '待处理' },
        { value: 'processing', label: '处理中' },
        { value: 'shipped', label: '已发货' },
        { value: 'in_transit', label: '运输中' },
        { value: 'out_for_delivery', label: '派送中' },
        { value: 'delivered', label: '已送达' },
        { value: 'cancelled', label: '已取消' }
      ],
      statusTypeMap: {
        pending: 'info',
        processing: 'primary',
        shipped: 'success',
        in_transit: 'warning',
        out_for_delivery: 'warning',
        delivered: 'success',
        cancelled: 'danger'
      },
      statusLabelMap: {
        pending: '待处理',
        processing: '处理中',
        shipped: '已发货',
        in_transit: '运输中',
        out_for_delivery: '派送中',
        delivered: '已送达',
        cancelled: '已取消'
      },
      statusDialogVisible: false,
      statusForm: {
        id: undefined,
        status: '',
        remark: ''
      },
      currentStatus: '',
      nextStatusOptions: {}
    }
  },
  created() {
    this.getList()
    this.getLogisticsCompanies()
  },
  methods: {
    getList() {
      this.listLoading = true
      http.expressOrder.page(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.count
        this.listLoading = false
      })
    },
    getLogisticsCompanies() {
      http.logisticsCompany.simpleList().then(response => {
        this.logisticsCompanies = response.data.rows
      })
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push('/order/create')
    },
    handleDetail(row) {
      this.$router.push({ path: '/order/detail/' + row.id })
    },
    handleUpdateStatus(row) {
      this.statusForm.id = row.id
      this.statusForm.status = ''
      this.statusForm.remark = ''
      this.currentStatus = row.status

      // 根据当前状态设置可选的下一状态
      this.setNextStatusOptions(row.status)

      this.statusDialogVisible = true
    },
    setNextStatusOptions(currentStatus) {
      // 根据当前状态设置可以更新的下一状态
      switch(currentStatus) {
        case 'pending':
          this.nextStatusOptions = {
            processing: '处理中',
            cancelled: '已取消'
          }
          break
        case 'processing':
          this.nextStatusOptions = {
            shipped: '已发货',
            cancelled: '已取消'
          }
          break
        case 'shipped':
          this.nextStatusOptions = {
            in_transit: '运输中',
            cancelled: '已取消'
          }
          break
        case 'in_transit':
          this.nextStatusOptions = {
            out_for_delivery: '派送中'
          }
          break
        case 'out_for_delivery':
          this.nextStatusOptions = {
            delivered: '已送达'
          }
          break
        default:
          this.nextStatusOptions = {}
      }
    },
    submitStatusUpdate() {
      if (!this.statusForm.status) {
        this.$message({
          message: '请选择新状态',
          type: 'warning'
        })
        return
      }

      const data = {
        id: this.statusForm.id,
        status: this.statusForm.status,
        remark: this.statusForm.remark
      }

      http.expressOrder.updateStatus(data).then(() => {
        this.statusDialogVisible = false
        this.$notify({
          title: '成功',
          message: '订单状态更新成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    }
  }
}
</script>

<style scoped>
.link-type {
  color: #409EFF;
  cursor: pointer;
}
</style>
